package com.macro.mall.portal.service.impl;

import com.github.pagehelper.PageHelper;
import com.macro.mall.domain.HomeContentResult;
import com.macro.mall.domain.ProductCategoryResult;
import com.macro.mall.mapper.*;
import com.macro.mall.model.*;
import com.macro.mall.dao.HomeDao;
import com.macro.mall.portal.service.HomeService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 首页内容管理Service实现类
 * Created by macro on 2019/1/28.
 */
@Log4j2
@Service
public class HomeServiceImpl implements HomeService {
    @Autowired
    private SmsHomeAdvertiseMapper advertiseMapper;
    @Autowired
    private HomeDao homeDao;
    @Autowired
    private PmsProductMapper pmsProductMapper;
    @Autowired
    private SmsFlashPromotionMapper flashPromotionMapper;
    @Autowired
    private SmsFlashPromotionSessionMapper promotionSessionMapper;
    @Autowired
    private PmsProductCategoryMapper productCategoryMapper;
    @Autowired
    private CmsSubjectMapper subjectMapper;

    @Override
    public HomeContentResult content() {
        HomeContentResult result = new HomeContentResult();
        //获取新品好物商品
        result.setNewProductList(newProductList(null, null, null, 0, 4));
        //获取好换商品
        result.setProductList(recommendProductList(0, 4));
        //获取首页广告
        result.setAdvertiseList(getHomeAdvertiseList());
        // 商品分类
        result.setProductCategory(getProductCateList());
        return result;
    }

    @Override
    public List<PmsProduct> newProductList(String maxPrice, String minPrice, String productCategoryName, Integer pageSize, Integer pageNum) {
        return homeDao.getNewProductList(maxPrice, minPrice, productCategoryName, pageNum, pageSize);
    }

    @Override
    public Integer newProductListCount(String maxPrice, String minPrice, String productCategoryName) {
        return homeDao.getNewProductListCount(maxPrice, minPrice, productCategoryName);
    }

    @Override
    public Integer recommendProductListCount() {
        return homeDao.getRecommendProductListCount();
    }

    @Override
    public List<PmsProduct> recommendProductList(Integer pageSize, Integer pageNum) {
        return homeDao.getRecommendProductList(pageNum, pageSize);
    }

    @Override
    public ArrayList<ProductCategoryResult> getProductCateList() {
        PmsProductCategoryExample example = new PmsProductCategoryExample();
        example.createCriteria()
                .andShowStatusEqualTo(1);
        List<PmsProductCategory> pmsProductCategories = productCategoryMapper.selectByExample(example);

        ArrayList<ProductCategoryResult> productCategoryResults = new ArrayList<>();
        for (PmsProductCategory pmsProductCategory : pmsProductCategories) {
            Integer level = pmsProductCategory.getLevel();
            if (0 == level) {
                ProductCategoryResult productCategoryResult = new ProductCategoryResult();
                BeanUtils.copyProperties(pmsProductCategory, productCategoryResult);
                productCategoryResult.setItems(new ArrayList<ProductCategoryResult>());
                productCategoryResults.add(productCategoryResult);
                setChildes(productCategoryResult, pmsProductCategories);
            }
        }
        log.info("productCategoryResults.size():{}", productCategoryResults.size());
        return productCategoryResults;
    }

    @Override
    public PmsProduct productList(Long id) {
        PmsProductExample pmsProductExample = new PmsProductExample();
        PmsProductExample.Criteria criteria = pmsProductExample.createCriteria();
//        criteria.andDeleteStatusEqualTo()


        return pmsProductMapper.selectByPrimaryKey(id);
    }

    private void setChildes(ProductCategoryResult productCategoryResult, List<PmsProductCategory> productCateList) {
        for (PmsProductCategory productCategory : productCateList) {
            if (productCategoryResult.getId().equals(productCategory.getParentId())) {
                ProductCategoryResult categocryResult = new ProductCategoryResult();
                BeanUtils.copyProperties(productCategory, categocryResult);
                productCategoryResult.getItems().add(categocryResult);
            }
        }
    }

    @Override
    public List<CmsSubject> getSubjectList(Long cateId, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        CmsSubjectExample example = new CmsSubjectExample();
        CmsSubjectExample.Criteria criteria = example.createCriteria();
        criteria.andShowStatusEqualTo(1);
        if (cateId != null) {
            criteria.andCategoryIdEqualTo(cateId);
        }
        return subjectMapper.selectByExample(example);
    }

    private List<SmsHomeAdvertise> getHomeAdvertiseList() {
        SmsHomeAdvertiseExample example = new SmsHomeAdvertiseExample();
        example.createCriteria().andTypeEqualTo(1).andStatusEqualTo(1);
        example.setOrderByClause("sort desc");
        return advertiseMapper.selectByExample(example);
    }

//    private HomeFlashPromotion getHomeFlashPromotion() {
//        HomeFlashPromotion homeFlashPromotion = new HomeFlashPromotion();
//        //获取当前秒杀活动
//        Date now = new Date();
//        SmsFlashPromotion flashPromotion = getFlashPromotion(now);
//        if (flashPromotion != null) {
//            //获取当前秒杀场次
//            SmsFlashPromotionSession flashPromotionSession = getFlashPromotionSession(now);
//            if (flashPromotionSession != null) {
//                homeFlashPromotion.setStartTime(flashPromotionSession.getStartTime());
//                homeFlashPromotion.setEndTime(flashPromotionSession.getEndTime());
//                //获取下一个秒杀场次
//                SmsFlashPromotionSession nextSession = getNextFlashPromotionSession(homeFlashPromotion.getStartTime());
//                if (nextSession != null) {
//                    homeFlashPromotion.setNextStartTime(nextSession.getStartTime());
//                    homeFlashPromotion.setNextEndTime(nextSession.getEndTime());
//                }
//                //获取秒杀商品
//                List<FlashPromotionProduct> flashProductList = homeDao.getFlashProductList(flashPromotion.getId(), flashPromotionSession.getId());
//                homeFlashPromotion.setProductList(flashProductList);
//            }
//        }
//        return homeFlashPromotion;
//    }

    //获取下一个场次信息
//    private SmsFlashPromotionSession getNextFlashPromotionSession(Date date) {
//        SmsFlashPromotionSessionExample sessionExample = new SmsFlashPromotionSessionExample();
//        sessionExample.createCriteria()
//                .andStartTimeGreaterThan(date);
//        sessionExample.setOrderByClause("start_time asc");
//        List<SmsFlashPromotionSession> promotionSessionList = promotionSessionMapper.selectByExample(sessionExample);
//        if (!CollectionUtils.isEmpty(promotionSessionList)) {
//            return promotionSessionList.get(0);
//        }
//        return null;
//    }

    //根据时间获取秒杀活动
//    private SmsFlashPromotion getFlashPromotion(Date date) {
//        Date currDate = DateUtil.getDate(date);
//        SmsFlashPromotionExample example = new SmsFlashPromotionExample();
//        example.createCriteria()
//                .andStatusEqualTo(1)
//                .andStartDateLessThanOrEqualTo(currDate)
//                .andEndDateGreaterThanOrEqualTo(currDate);
//        List<SmsFlashPromotion> flashPromotionList = flashPromotionMapper.selectByExample(example);
//        if (!CollectionUtils.isEmpty(flashPromotionList)) {
//            return flashPromotionList.get(0);
//        }
//        return null;
//    }

    //根据时间获取秒杀场次
//    private SmsFlashPromotionSession getFlashPromotionSession(Date date) {
//        Date currTime = DateUtil.getTime(date);
//        SmsFlashPromotionSessionExample sessionExample = new SmsFlashPromotionSessionExample();
//        sessionExample.createCriteria()
//                .andStartTimeLessThanOrEqualTo(currTime)
//                .andEndTimeGreaterThanOrEqualTo(currTime);
//        List<SmsFlashPromotionSession> promotionSessionList = promotionSessionMapper.selectByExample(sessionExample);
//        if (!CollectionUtils.isEmpty(promotionSessionList)) {
//            return promotionSessionList.get(0);
//        }
//        return null;
//    }
}
